//
//  ProductInfoHandle.m
//  sensen_ios
//
//  Created by pro on 16/3/9.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ProductInfoHandle.h"

@implementation ProductInfoHandle


#pragma mark - 规格模块辅助方法
+ (NSString *)getCurrCollectSkuId:(NSIndexPath *)indexPath skuIdModel:(SkuIdModel *)model {
    //拼接
    NSString *keyID = [NSString stringWithFormat:@"%@:%@",model.sid,model.vidAry[indexPath.row]];
    return keyID;
}

// 没有选择collection时数组的保存东西
+ (NSString *)getDefaultNoIDString{
    
    return @"sku_id";
}

// skuIDArray中非当前点击的其他所有类型（如，1：2；2：1；3：1，当前点击  第三分区），数组包含（1：1，2：2），
+ (NSArray *)getSelectSkuIDList:(NSArray *)skuIDArray except:(NSInteger)section{
    
    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
    for (NSInteger i = 0; i < skuIDArray.count; i ++) {
        NSString * sku_id = [skuIDArray objectAtIndex:i];
        if (i != section) {
            [array addObject:sku_id];
        }
    }
    return array;
}

// selectedSkuIdAry:是除了未赋值的所有skuId的数组
// 而_skuSelectIdArray是包含了未赋值的所有的skuId的数组
// 根据_skuSelectIdArray来获得 selectedSkuIdAry
+ (NSArray *)getSelectedSkuIdAry:(NSArray *)skuSelectIdArray{
    
    NSMutableArray *selectedSkuIdAry = [[NSMutableArray alloc] initWithCapacity:0];
    for (NSString *string in skuSelectIdArray) {
        if (![string isEqualToString:[self getDefaultNoIDString]]) {
            [selectedSkuIdAry addObject:string];
        }
    }
    return selectedSkuIdAry;
}


/**
 *  判断当前选中的规格项是否能拼接成完成的商品规格id（sku_id）
 *
 *  @param selectedSkuIdArray 选中的skuId数组（里面可能包含正确的skuid(1：1)，也可能包含未赋值的）
 *  @param complete           判断完成后回调
 *  @param noSelectAry        返回没有选中的组索引的数组
 */
+ (void)isUseableSkuIdArray:(NSArray *)selectedSkuIdArray
                   complete:(void(^)(BOOL isable))complete
                noSelectAry:(void(^)(NSArray *notSeletSectionArray))noSelectAry {
    
    BOOL able = YES;
    
    NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:0];
    
    for (NSInteger i = 0; i < selectedSkuIdArray.count; i ++) {
        NSString *skuId =  selectedSkuIdArray[i];
        
        if ([skuId isEqual:[self getDefaultNoIDString]]) { // skuId(1：1)只要有一个是没有赋值，就不行
            able = NO;
            [arr addObject:[NSString stringWithFormat:@"%ld",(long)i]]; // 返回没有选规格的组索引
        }
    }
    
    complete(able);
    
    if (able == NO) {
        noSelectAry(arr);
    }
}


// 找到预测分区下不可点击的skuID，（根据skuID可以找到规格下标）
+(void)forecastSkuIdIsable:(ByModelProductDetail *)detailProdtInfo
          selectedSkuIdAry:(NSArray *)selectSkuIDArray
                   section:(NSInteger)section
              disAbleBlock:(void(^)(NSArray *))disTouchSkuIDArray {
    
    // 需要预测是否可点击的分组的 skuId
    SkuIdModel *skuIdModel = detailProdtInfo.skuIdAry[section];
    
    // 获得 包含了已经选择的分区sku_id 的规格模型数组，在进一步判断是否有库存
    NSArray *containArray = [self getContainSkuModel:detailProdtInfo.skuAry selectedSkuIDAry:selectSkuIDArray];
    
    // 没有数量的skuId
    NSArray *noQuantiyArray = [self getNoQuantitySkuModel:containArray skuIdModel:skuIdModel];
    
    // 回调block
    disTouchSkuIDArray(noQuantiyArray);
}
// 找到预测分区下可点击的skuID，（根据skuID可以找到规格下标）
+(void)forecastSkuIdIsable:(ByModelProductDetail *)detailProdtInfo
          selectedSkuIdAry:(NSArray *)selectSkuIDArray
                   section:(NSInteger)section
                 ableBlock:(void(^)(NSArray *))ableTouchSkuIDArray {
    
    // 需要预测是否可点击的分组的 skuId
    SkuIdModel *skuIdModel = detailProdtInfo.skuIdAry[section];
    
    // 获得 包含了已经选择的分区sku_id 的规格模型数组，在进一步判断是否有库存
    NSArray *containArray = [self getContainSkuModel:detailProdtInfo.skuAry selectedSkuIDAry:selectSkuIDArray];
    
    // 没有数量的skuId
    NSArray *hasQuantiyArray = [self getHasQuantitySkuModel:containArray skuIdModel:skuIdModel];
    
    // 回调block
    ableTouchSkuIDArray(hasQuantiyArray);
}


// 获得数组中包含指定skuID（49：131）的skuModel，
+ (NSArray *)getContainSkuModel:(NSArray *)skuAry
               selectedSkuIDAry:(NSArray *)selectedIDAry {
    
    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
    for (SkuModel *skuModel in skuAry) {
        BOOL isOk = YES;
        for (NSString *skuId in selectedIDAry) {
            if (![skuModel.sku_id containsString:skuId] && ![skuModel.sku_id isEqualToString:skuId]) {
                isOk = NO;
            }
        }
        if (isOk == YES) {
            [array addObject:skuModel];
        }
    }
    return array;
}
// 预测分区内没有选择的没有数量的skuID  1：2
+ (NSArray *)getNoQuantitySkuModel:(NSArray *)pridicDisArray
                        skuIdModel:(SkuIdModel *)skuIdModel {
    
    // 库存为0的规格model（skuModel）
    NSMutableArray *noQuantityArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (SkuModel *skuModel in pridicDisArray) {
        if ([skuModel.quantity integerValue] == 0) {
            [noQuantityArray addObject:skuModel];
        }
    }
    
    // skuID（如49:135）
    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
    for (NSString *key in skuIdModel.keyValueList) {
        // 遍历没有库存的商品
        for (SkuModel *skuModel in noQuantityArray) {
            if ([skuModel.sku_id containsString:key]) {
                [array addObject:key];
                break;
            }
        }
    }
    
    // 返回需要预测是否可点击的那一组中不可点击的skuId（如49:135）
    return array;
}
// 预测分区内没有选择的没有数量的skuID  1：2
+ (NSArray *)getHasQuantitySkuModel:(NSArray *)pridicDisArray
                         skuIdModel:(SkuIdModel *)skuIdModel {
    
    // 库存为0的规格model（skuModel）
    NSMutableArray *hasQuantityArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (SkuModel *skuModel in pridicDisArray) {
        if ([skuModel.quantity integerValue] != 0) {
            [hasQuantityArray addObject:skuModel];
        }
    }
    
    // skuID（如49:135）
    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
    for (NSString *key in skuIdModel.keyValueList) {
        // 遍历没有库存的商品
        for (SkuModel *skuModel in hasQuantityArray) {
            if ([skuModel.sku_id containsString:key]) {
                [array addObject:key];
                break;
            }
        }
    }
    
    // 返回需要预测是否可点击的那一组中不可点击的skuId（如49:135）
    return array;
}

// 根据选中的id 字符串拼接完整的sku_id （比如选中的是48:134 49:135，即数组中有两个元素则拼接成 48:134;49:135）
+ (NSString *)getFullSkuId:(NSArray *)selectIdAry {
    
    // 获得选中后的规格ID
    NSString *fullSkuId = [selectIdAry componentsJoinedByString:@";"];
    fullSkuId = [NSString stringWithFormat:@"%@;",fullSkuId];
    return fullSkuId;
    
}
// 根据完整的sku_id 从商品数组（skuAry）中找到该商品，并返回
+ (SkuModel *)findSkuModelInSkuAry:(NSArray<SkuModel *> *)skuAry
                         fullSkuId:(NSString *)fullId {
    
    //重商品列表中查找符合标准的规格
    for (SkuModel *model in skuAry) {
        
        NSLog(@"%@,%@", model.sku_id, fullId);
        if ([model.sku_id isEqualToString:fullId] || [model.sku_id containsString:fullId] || [fullId containsString:model.sku_id]) {
            
            return model;
        }
    }
    return nil;
}



@end
